package presentation;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;



public class ExitListener extends WindowAdapter {

  @Override
  public void windowClosing(WindowEvent event) {
	PresentationController pc = PresentationController.getInstance();
	if(pc.getState() != 0 && pc.getState() != 1) {
	    int a = JOptionPane.showConfirmDialog(null,"Do you want to save the current text?","Save",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
	    if(a == JOptionPane.YES_OPTION) {
		JTextArea aux = new JTextArea();
		aux.setText(pc.getText());
		pc.save(aux);
                pc.exit();
	    }
	    else if(a == JOptionPane.CANCEL_OPTION) {
		pc.goToEdit();
	    }
            else {
                pc.exit();
            }
	}
        else System.exit(0);
    }
}
